/*
 * Copyright 2011 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package nm.aleksey.client;

import nm.aleksey.shared.TableRequestFactory;

import com.google.gwt.core.client.GWT;
import com.google.web.bindery.event.shared.EventBus;
import com.google.web.bindery.event.shared.SimpleEventBus;

/**
 * Default implementation of {@link ClientFactory}. Used by desktop version.
 */
class ClientFactoryImpl implements ClientFactory {

  /**
   * The URL argument used to enable or disable local storage.
   */
  // private static final String STORAGE_URL_ARG = "storage";
  // private static final Logger log = Logger.getLogger(SimpleTable.class
  // .getName());

  private final EventBus eventBus = new SimpleEventBus();
  // = new SimpleEventBus();
  // private final PlaceController placeController = new
  // PlaceController(eventBus);
  private final TableRequestFactory requestFactory;

  // private final Storage localStorage;
  // private final TaskProxyLocalStorage taskProxyLocalStorage;
  // private TaskEditView taskEditView;
  // private TaskListView taskListView;
  // private ActivityManager activityManager;

  // private final AppPlaceHistoryMapper historyMapper = GWT
  // .create(AppPlaceHistoryMapper.class);

  /**
   * The stock GWT class that ties the PlaceController to browser history,
   * configured by our custom {@link #historyMapper}.
   */
  // private final PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(
  // historyMapper);

  // private TaskReadView taskReadView;

  public ClientFactoryImpl() {
    // GWT.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
    // @Override
    // public void onUncaughtException(Throwable e) {
    // log.log(Level.SEVERE, e.getMessage(), e);
    // }
    // });

    // RequestTransport requestTransport = new
    // GaeAuthRequestTransport(eventBus);
    requestFactory = GWT.create(TableRequestFactory.class);
    requestFactory.initialize(eventBus);
    // requestFactory.initialize(eventBus, requestTransport);

    // Initialize local storage.
    // String storageUrlValue = Window.Location.getParameter(STORAGE_URL_ARG);
    // if (storageUrlValue == null || storageUrlValue.startsWith("t")) {
    // localStorage = Storage.getLocalStorageIfSupported();
    // } else {
    // localStorage = null;
    // }
    // taskProxyLocalStorage = new TaskProxyLocalStorage(localStorage);
  }

  /**
   * ActivityMapper determines an Activity to run for a particular place,
   * configures the {@link #getActivityManager()}
   */
  // protected ActivityMapper createActivityMapper() {
  // return new AppActivityMapper(this);
  // }

  /**
   * Create a {@link TaskEditView}.
   * 
   * @return a new {@link TaskEditView}
   */
  // protected TaskEditView createTaskEditView() {
  // return new DesktopTaskEditView();
  // }

  /**
   * Create a {@link TaskListView}.
   * 
   * @return a new {@link TaskListView}
   */
  // protected TaskListView createTaskListView() {
  // return new DesktopTaskListView();
  // }

  // protected TaskReadView createTaskReadView() {
  // return new DesktopTaskReadView();
  // }

  /**
   * Owns a panel in the window, in this case the entire {@link #shell}.
   * Monitors the {@link #eventBus} for
   * {@link com.google.gwt.place.shared.PlaceChangeEvent PlaceChangeEvent}s
   * posted by the {@link #placeController}, and chooses what
   * {@link com.google.gwt.activity.shared.Activity Activity} gets to take over
   * the panel at the current place. Configured by the
   * {@link #createActivityMapper()}.
   */
  // protected ActivityManager getActivityManager() {
  // if (activityManager == null) {
  // activityManager = new ActivityManager(createActivityMapper(), eventBus);
  // }
  // return activityManager;
  // }

  @Override
  public App getApp() {
    return new App(this);
  }

  @Override
  public EventBus getEventBus() {
    return eventBus;
  }

  // private Storage getLocalStorageIfSupported() {
  // return localStorage;
  // }

  // @Override
  // public PlaceController getPlaceController() {
  // return placeController;
  // }

  @Override
  public TableRequestFactory getRequestFactory() {
    return requestFactory;
  }

  // public MobileWebAppShell getShell() {
  // if (shell == null) {
  // shell = createShell();
  // }
  // return shell;
  // }
  //
  // public TaskEditView getTaskEditView() {
  // if (taskEditView == null) {
  // taskEditView = createTaskEditView();
  // }
  // return taskEditView;
  // }
  //
  // public TaskListView getTaskListView() {
  // if (taskListView == null) {
  // taskListView = createTaskListView();
  // }
  // return taskListView;
  // }
  //
  // public TaskProxyLocalStorage getTaskProxyLocalStorage() {
  // return taskProxyLocalStorage;
  // }
  //
  // public TaskReadView getTaskReadView() {
  // if (taskReadView == null) {
  // taskReadView = createTaskReadView();
  // }
  // return taskReadView;
  // }
}
